Introduction to Machine Learning

Unit 05: Overfitting, Underfitting, Bias-Variance, Fβ & Imbalanced Classes

1. Introduction

Accuracy is a dangerously misleading metric on imbalanced datasets, and "perfect training accuracy" is a warning sign, not a win. This unit covers two fundamental pillars of applied ML: the bias-variance tradeoff (diagnosing underfitting vs. overfitting with learning curves), and the precision/recall/Fβ family of metrics for class-imbalanced problems, plus a quick introduction to grid search as a hyperparameter-tuning tool.

Learning Objectives

2. Theory

2.1 Model Complexity, Generalization, and the KNN Complexity Ladder

Generalization = ability to perform well on unseen data. Model complexity = flexibility to fit arbitrary data patterns.

K value (KNN)# Effective ParametersModel ComplexityTypical Behavior
K = 10 (stores data)HighestMemorizes every training point
K = 30HighVery flexible, jagged boundaries
K = 100MediumModerately smooth
K = 1000LowSmooth, simple boundaries
K = n (all points)0LowestConstant majority-class baseline

2.2 Underfitting vs. Overfitting

Underfitting (Too Simple)
Overfitting (Too Complex)
Just Right

2.3 The Bias-Variance Decomposition

\( \text{Expected Test Error} = \text{Bias}^2 + \text{Variance} + \text{Irreducible Noise} \)

2.4 Learning Curves — Two X-Axis Families

Two complementary plotting habits diagnose different failure modes:

X = Training Set Size
X = Model Complexity

Plot train error (decreasing curve) and validation error (decreasing then plateau) against the number of training rows:

For KNN, vary k (small k = more complex) on the X axis against train + val accuracy on the Y axis.

Accuracy versus model complexity Training accuracy rises as model complexity increases, while validation accuracy rises to a maximum sweet spot and then falls. Accuracy vs. model complexity Training improves continuously; validation performance peaks at the right level of complexity. 1.0 0.5 0.0 Accuracy Training accuracy rises Validation accuracy Sweet spot Validation accuracy is maximized k = 1 k = 10 k = n complex simple Model complexity (k small → large)

Pick the complexity where validation set accuracy is maximal (or validation loss minimal).

2.5 Grid Search for Hyperparameter Tuning

Grid search = brute-force exhaustive sweep over a user-specified Cartesian grid of hyperparameter combinations. For each combination, run K-Fold CV and record its mean CV score; then pick the combination with the best score.

from sklearn.model_selection import GridSearchCV from sklearn.neighbors import KNeighborsClassifier param_grid = { 'n_neighbors': [3, 5, 7, 11, 15, 19, 25, 31], 'weights': ['uniform', 'distance'], 'metric': ['euclidean', 'manhattan'], } gs = GridSearchCV(KNeighborsClassifier(), param_grid, cv=10, scoring='accuracy', n_jobs=-1) gs.fit(X_trainval, y_trainval) print("Best CV score %.3f" % gs.best_score_) print("Best params:", gs.best_params_) final_model = gs.best_estimator_ # pre-refit on full trainval print("Test accuracy %.3f" % final_model.score(X_test, y_test))

Learning curves vs. Grid search

2.6 The Class Imbalance Problem — Why Accuracy Fails

🚨 The "99% Accuracy" Fraud Detector Trap

Dataset: 9,990 legitimate transactions (= Class 0), 10 fraud (= Class 1). Total n = 10,000.

A trivial model that predicts "Legitimate" for every single transaction achieves 9,990 / 10,000 = 99.9% accuracy — and 0 frauds caught. High accuracy, completely useless.

PredictedTotal
0 (Legit)1 (Fraud)
True09,990 (TN)0 (FP)9,990
1 (Fraud)10 (FN)0 (TP)10
Total9,990010,000

Accuracy is always reported, but never trusted alone on imbalanced tasks.

2.7 Confusion Matrix Terminology (Medical framing is memorable)

2.8 Precision, Recall, and the Fβ Family

\( \text{Recall (Sensitivity, TPR)} = \frac{TP}{TP + FN} \;\;=\;\; \frac{\text{caught positives}}{\text{all real positives}} \)

\( \text{Precision (PPV)} = \frac{TP}{TP + FP} \;\;=\;\; \frac{\text{caught positives}}{\text{all predicted positive}} \)

\( F_1 = 2 \cdot \frac{\text{Precision} \cdot \text{Recall}}{\text{Precision} + \text{Recall}} \quad \text{(harmonic mean)} \)

\( F_\beta = (1 + \beta^2) \cdot \frac{\text{Precision} \cdot \text{Recall}}{\beta^2 \cdot \text{Precision} + \text{Recall}} \)

2.9 Why Harmonic Mean, not Arithmetic?

Intuitive example: Precision = 100%, Recall = 50%

Arithmetic mean = (1.0 + 0.5)/2 = 0.75. Sounds good! But wait — a model that only flags one very-obvious positive (so no FP = 100% precision) and misses half of all real positives is not a 75% model. It's a coward.

Harmonic mean = 2 × 1.0 × 0.5 / (1.0 + 0.5) = 1.0/1.5 = 0.667. It is always ≤ the arithmetic mean, and it is dragged toward the minimum of the two — exactly what we want so we can't game the metric by doing great on one and terrible on the other.

Numeric check: (Precision=0.01, Recall=0.99). Arithmetic mean = 0.50 (sounds fine!). Harmonic mean ≈ 0.02 (correctly terrible, since you're flagging everything and still barely being right 1% of the time!).

3. Interactive Examples

Example 1: Diagnose the Learning Curve

Curve Detective 🕵️

Three learning-curve scenarios. Match each to its diagnosis and recommendation:

  1. Scenario A: Training accuracy 99%, validation accuracy 72%, large gap. Adding more training data doesn't shrink the gap significantly.
  2. Scenario B: Training accuracy 68%, validation accuracy 66%, both low and close together. Adding more data barely helps.
  3. Scenario C: Training accuracy starts at 99% on 100 samples and drifts to 90% by 10,000 samples. Validation accuracy starts at 55%, rises monotonically, and is still climbing at 10,000 samples (not flat yet).
  1. High Variance / Overfitting. Recommendation: make model simpler (increase k for KNN, more regularization, add feature selection, remove noisy features).
  2. High Bias / Underfitting. Recommendation: make model more complex (decrease k, add features, decrease regularization, switch to richer class of model).
  3. Both curves still converging — need more data. Acquire additional labeled rows.

Example 2: Imbalanced Dataset Metric Calculation

Classifier on medical diagnosis: 8 sick / 1000 patients total (imbalanced!). Confusion matrix below.

PredictedTotal
Sick (+)Healthy (−)
TrueSickTP = 8FN = 210
HealthyFP = 48TN = 942990
Total569441000
Compute Accuracy, Precision, Recall, F1 step by step (click to reveal)
\( \text{Accuracy} = \frac{8 + 942}{1000} = \frac{950}{1000} = \mathbf{95.0\%} \) \( \text{Recall (TPR)} = \frac{TP}{TP+FN} = \frac{8}{10} = \mathbf{80\%} \) \( \text{Precision} = \frac{TP}{TP+FP} = \frac{8}{56} \approx \mathbf{14.3\%} \) \( F_1 = 2 \cdot \frac{0.1429 \cdot 0.80}{0.1429 + 0.80} \approx 2 \cdot \frac{0.1143}{0.9429} \approx \mathbf{0.242} (24.2\%)

Interpretation: 95% accuracy hides the poor classifier. F1 of 0.24 honestly reflects the terrible precision (48 healthy people were told they are sick). For a disease screening task, Recall ≥ 95% is often mandated as a minimum KPI before Precision is even looked at — so this model would not pass go.

Example 3: β Parameter Tuning

For each task, pick β ∈ {0.3, 1, 4} (low, equal, high) to weight Precision vs. Recall appropriately, then give a 1-sentence reason:

  1. Email spam filter: "Spam" = positive class. Blocking a real job-offer email is much worse than letting a spam email through.
  2. Airport bomb-detection scanner: "Bomb present" = positive class. A missed bomb is catastrophic; a false positive just leads to a bag re-check.
  3. Generic document classification (balanced classes): No obvious asymmetry between FP and FN.
  1. β = 0.3 (low, emphasize Precision). Penalize false positives (ham falsely flagged as spam) much more heavily than missed spam.
  2. β = 4 (high, emphasize Recall). If a real bomb has a 99% chance of being caught we tolerate a moderate false-alarm rate to get that guarantee.
  3. β = 1 (standard F1). No cost asymmetry → weight both metrics equally; the harmonic mean keeps both honest.

4. Numerical Solutions

Problem 1: KNN Complexity Curve by Hand

On a small 2-D toy binary problem, you test KNN with k = 1, 3, 7, 15 and measure both training accuracy and 5-fold CV (validation) accuracy: {k, train, CV} triples are {1, 1.00, 0.62}, {3, 0.95, 0.78}, {7, 0.88, 0.85}, {15, 0.78, 0.77}.

  1. Identify which k values show symptoms of overfitting, underfitting, and "just right".
  2. Which k should you pick for deployment? Why?
  3. Sketch the qualitative train and CV curves on scratch paper and confirm the "inverted U" CV shape is present.
📘 Full solution

(a) k=1: train accuracy 100% (memorized) — CV only 62% with big gap → classic overfitting / high variance. k=15: both errors are fairly high but close together → underfitting / high bias (too smooth, ignoring local structure). k=3 & k=7: moving toward just right as k rises to 7.

(b) Pick k = 7. It has the maximum cross-validation (validation) accuracy = 85%, with train (88%) and CV (85%) only 3 pp apart → low gap, low overfit.

(c) CV accuracy: k=1 → 0.62, k=3 → 0.78, k=7 → 0.85 (peak!), k=15 → 0.77 (falling back). That inverted-U shape is the complexity curve in action.

Problem 2: Full Confusion Matrix Derivation for Imbalanced Binary Classification

Classifier run on n = 500 samples, positive rate = 10% (50 sick / 450 healthy). Results: 40 sick correctly caught, 90 healthy incorrectly flagged.

  1. Fill in every cell of the confusion matrix (TP / FN / FP / TN).
  2. Compute Accuracy, Recall, Precision, F1.
  3. How would F2 (β = 2) differ from F1 here? Calculate F2 and compare directionally.
📘 Step-by-step solution

(a) True positives: 40. Total real positives 50 → FN = 10. FP = 90 given. Total healthy = 450 → TN = 450 − 90 = 360. Matrix: TP 40 / FN 10 / FP 90 / TN 360.

(b)

\( \text{Acc} = \frac{40 + 360}{500} = \frac{400}{500} = 80.0\% \) \( \text{Recall} = \frac{40}{40 + 10} = \frac{40}{50} = 80.0\% \) \( \text{Precision} = \frac{40}{40 + 90} = \frac{40}{130} \approx 30.8\% \) \( F_1 = 2 \cdot \frac{0.3077 \cdot 0.80}{0.3077 + 0.80} \approx \frac{0.4923}{1.1077} \approx 0.444 \)

(c)

\( F_2 = (1+4) \cdot \frac{P \cdot R}{4P + R} = 5 \cdot \frac{0.2462}{1.2308 + 0.80} \approx 5 \cdot \frac{0.2462}{2.0308} \approx 0.606

F2 (≈ 0.606) is substantially higher than F1 (≈ 0.444) because β=2 up-weights Recall, which this model does relatively well on (80%), while caring less about its poor Precision (30.8%). The "all caught but noisy" character of the model is rewarded as β grows.

Problem 3: Grid Search Combinatorics

GridSearchCV with param_grid = { n_neighbors: [5, 11, 19, 27, 35], weights: ['uniform', 'distance'], metric: ['euclidean', 'manhattan', 'chebyshev'] }. Stratified 5-fold CV.

  1. How many distinct hyperparameter combinations?
  2. Total distinct classifier fits (not counting final refit)?
  3. If a single KNN fit takes 0.2 seconds on this dataset, roughly what wall-clock runtime with n_jobs = -1 on an 8-CPU machine?
📘 Step-by-step solution

(a) Cartesian product: 5 k values × 2 weights × 3 metrics = 30 combinations.

(b) Each combination has 5 CV folds → 5 fits. 30 × 5 = 150 fits (plus 1 final refit on winner → 151 total).

(c) Sequential: 150 × 0.2s = 30 s. With 8 CPUs in parallel: ~30/8 ≈ 3.75 seconds (plus small overhead — very fast!). This is one of grid search's advantages — it's embarrassingly parallel.

5. Try It Yourself

Problem 1 — Learning Curve Prescription

A neural network gives training loss 0.001, validation loss 0.65. Your colleague suggests: "We just need more labeled data." Critique that suggestion by (a) naming the actual syndrome, then (b) giving three concrete interventions that address it directly, and (c) identifying one diagnostic observation on the curve that would actually justify "get more data."

(a) Classic high-variance / overfitting (huge train/val gap). (b) Three fixes from the menu: (i) simplify architecture (fewer layers/neurons), (ii) add dropout or weight regularization, (iii) add data augmentation / noise, (iv) apply early stopping, (v) feature selection to remove noisy inputs, (vi) decrease model complexity (e.g., bigger k if it were KNN). (c) "Need more data" is justified only when the validation loss curve is still decreasing at the right edge of the training-set-size X-axis and not yet plateaued. If it's flat with a big gap, more rows won't close it — the model is too flexible.

Problem 2 — Imbalance Metrics Practice

Ad-tech task: Out of 10,000 ad impressions, only 100 users click (positive). Our model predicts 150 clicks total. Of its 150 predicted clicks, 60 are real (TP) and 90 are wrong (FP). Of the 100 real clicks it missed 40 (FN).

  1. Fill in TP, FN, FP, TN.
  2. Calculate accuracy, precision, recall, F1, F_0.5 (β = 0.5 — penalize FP more).
  3. Interpret: Why is F_0.5 lower than F1 here?

(a) TP = 60; FN = 40; FP = 90; TN = 10000 − 60 − 40 − 90 = 9810.

(b)

\( \text{Acc} = \frac{60 + 9810}{10000} = 98.7\% \) \( P = 60/(60+90) = 40.0\%; \;\; R = 60/(60+40) = 60.0\% \) \( F_1 = 2 \cdot \frac{0.40 \cdot 0.60}{0.40 + 0.60} = 2 \cdot 0.24 = \mathbf{0.480} \) \( F_{0.5} = 1.25 \cdot \frac{0.24}{0.25 \cdot 0.40 + 0.60} = 1.25 \cdot \frac{0.24}{0.70} \approx \mathbf{0.429}

(c) F_0.5 ≈ 0.429 < F_1 ≈ 0.480 because β < 1 weights precision more heavily. This model has P = 40% (worse) and R = 60% (better) — downgrading the good metric and upgrading the bad one makes the harmonic mean drop, which correctly reflects the advertiser's pain of wasting budget on 90 non-clickers for every 60 real clicks.

Problem 3 — Grid Search with a Pipeline

You want to compare KNN hyperparameters but also need to standardize features. Why is Pipeline([('sc', StandardScaler()), ('clf', KNeighborsClassifier())]) required inside GridSearchCV instead of scaling once at the top level? Give the one-sentence leakage explanation, then write the param_grid format with pipeline namespaced keys.

Leakage explanation: Scaling before CV means each fold's StandardScaler was fit using test-fold rows as part of its mean/SD — the validation fold's distribution statistics leak into training, producing optimistically biased CV scores. The pipeline re-fits scaler + classifier on each fold's training split only, so CV is honest.

Namespaced grid format:

param_grid = { 'clf__n_neighbors': [3, 7, 11, 15, 21], 'clf__weights': ['uniform', 'distance'], 'clf__metric': ['euclidean', 'manhattan'], }

6. Interactive Quiz

Answer all 5 MCQs. Click on an option to get instant feedback.

Your score: 0 / 5

7. Key Takeaways

  1. Underfitting → high bias; overfitting → high variance. Use train-vs-val learning curves (both size-X and complexity-X) to diagnose which disease you have.
  2. Validation-accuracy peak = Optimum complexity. For KNN: find the k where CV score is maximized. Increase k → simpler model (less overfit, more underfit). Decrease k → opposite.
  3. When both curves are bad & close: need more complex model / more features. When they are far apart: need simpler model / more regularization / better feature selection. When both still climbing at max data: get more labeled rows.
  4. Accuracy lies on imbalanced tasks. Always report Precision, Recall, and F1 / Fβ alongside accuracy on any dataset where the minority class rate is ≪ 50%.
  5. F1 uses the harmonic mean, not arithmetic mean, exactly to prevent gaming one metric while failing the other. Harmonic mean ≤ arithmetic and always closer to the worse of the two values.
  6. β is the Recall-precision knob: β ≪ 1 → prioritize Precision (spam filter). β ≫ 1 → prioritize Recall (fraud/disease/bomb). β = 1 → standard F1 equal weighting.
  7. GridSearchCV with Pipelines does honest, parallel, multi-dimensional hyperparameter search. Always Pipeline-encapsulate scaling/encoding/selection with the classifier so CV folds don't leak.

8. Common Pitfalls

  1. Trusting accuracy only on imbalanced tasks. A "99% accurate" fraud detector can catch zero frauds. Always compute confusion matrix + PRF metrics.
  2. High training accuracy = goal. It is not. Training accuracy of 99% with val of 60% means you memorized noise. Stop celebrating, simplify the model.
  3. "Just get more data" for any problem. Works only when validation curve is still rising (not saturated). If there's a wide gap at saturation, data won't fix it — simpler model / regularization will.
  4. Using arithmetic mean of P and R. Gives 0.5 even when one is 100% and the other is 0%. Harmonic mean (F1/Fβ) honestly collapses to 0 in that case. It's not arbitrary — it's the correct aggregator.
  5. Grid search with pre-scaled data. Scaler fits leak test-fold information. Put scaler + classifier in a Pipeline inside GridSearchCV.
  6. Using F1 when cost asymmetry is huge. Use Fβ with a task-appropriate β. F1 is a lazy default when both errors cost the same — but they rarely do.

9. Resources